﻿// /*------------------------------------------------------------
// 工程名:    Ensemble
// 文件名:    ExtMethodsIEnumerable.cs
// 编写人:    yhh
// 编写日期: 下午 4:11
// -------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Text;

namespace Ensemble
{
    public static class ExtMethodsIEnumerable
    {
        public static string ToString<T>(this IEnumerable<T> collection, Func<T, string> toString, string separator)
        {
            var sb = new StringBuilder();
            foreach (var item in collection)
            {
                sb.Append(toString(item));
                sb.Append(separator);
            }
            return sb.ToString(0, Math.Max(0, sb.Length - separator.Length));
        }

        public static string ToString<T>(this IEnumerable<T> collection, string separator)
        {
            var sb = new StringBuilder();
            foreach (var item in collection)
            {
                sb.Append(item != null ? item.ToString() : "null");
                sb.Append(separator);
            }
            return sb.ToString(0, Math.Max(0, sb.Length - separator.Length));
        }
    }
}